using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace SnakeGame
{
    public partial class LeaderboardForm : Form
    {
        private Leaderboard leaderboard;
        private ComboBox difficultyComboBox;
        private ListBox leaderboardListBox;
        private Button closeButton;
        private Label titleLabel;

        public LeaderboardForm(Leaderboard leaderboard)
        {
            this.leaderboard = leaderboard;
            InitializeComponent();
            LoadLeaderboard();
        }

        private void InitializeComponent()
        {
            this.titleLabel = new Label();
            this.difficultyComboBox = new ComboBox();
            this.leaderboardListBox = new ListBox();
            this.closeButton = new Button();
            this.SuspendLayout();

            // titleLabel
            this.titleLabel.AutoSize = true;
            this.titleLabel.Font = new Font("微软雅黑", 16F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(134)));
            this.titleLabel.Location = new Point(12, 9);
            this.titleLabel.Name = "titleLabel";
            this.titleLabel.Size = new Size(100, 30);
            this.titleLabel.Text = "排行榜";

            // difficultyComboBox
            this.difficultyComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            this.difficultyComboBox.Font = new Font("微软雅黑", 10F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));
            this.difficultyComboBox.FormattingEnabled = true;
            this.difficultyComboBox.Items.AddRange(new object[] {
                "全部难度",
                "简单",
                "中等",
                "困难"});
            this.difficultyComboBox.Location = new Point(12, 50);
            this.difficultyComboBox.Name = "difficultyComboBox";
            this.difficultyComboBox.Size = new Size(150, 28);
            this.difficultyComboBox.TabIndex = 0;
            this.difficultyComboBox.SelectedIndex = 0;
            this.difficultyComboBox.SelectedIndexChanged += new EventHandler(this.difficultyComboBox_SelectedIndexChanged);

            // leaderboardListBox
            this.leaderboardListBox.Font = new Font("微软雅黑", 10F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));
            this.leaderboardListBox.FormattingEnabled = true;
            this.leaderboardListBox.ItemHeight = 20;
            this.leaderboardListBox.Location = new Point(12, 90);
            this.leaderboardListBox.Name = "leaderboardListBox";
            this.leaderboardListBox.Size = new Size(460, 300);
            this.leaderboardListBox.TabIndex = 1;

            // closeButton
            this.closeButton.BackColor = Color.LightGray;
            this.closeButton.FlatStyle = FlatStyle.Flat;
            this.closeButton.Font = new Font("微软雅黑", 10F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));
            this.closeButton.Location = new Point(200, 400);
            this.closeButton.Name = "closeButton";
            this.closeButton.Size = new Size(80, 35);
            this.closeButton.TabIndex = 2;
            this.closeButton.Text = "关闭";
            this.closeButton.UseVisualStyleBackColor = false;
            this.closeButton.Click += new EventHandler(this.closeButton_Click);

            // LeaderboardForm
            this.AutoScaleDimensions = new SizeF(8F, 15F);
            this.AutoScaleMode = AutoScaleMode.Font;
            this.ClientSize = new Size(484, 450);
            this.Controls.Add(this.closeButton);
            this.Controls.Add(this.leaderboardListBox);
            this.Controls.Add(this.difficultyComboBox);
            this.Controls.Add(this.titleLabel);
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "LeaderboardForm";
            this.StartPosition = FormStartPosition.CenterParent;
            this.Text = "排行榜";
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private void LoadLeaderboard()
        {
            leaderboardListBox.Items.Clear();
            
            var entries = leaderboard.Entries;
            if (difficultyComboBox.SelectedIndex > 0)
            {
                var difficulty = (GameDifficulty)(difficultyComboBox.SelectedIndex - 1);
                entries = leaderboard.GetEntriesByDifficulty(difficulty);
            }

            if (entries.Count == 0)
            {
                leaderboardListBox.Items.Add("暂无记录");
                return;
            }

            for (int i = 0; i < entries.Count; i++)
            {
                var entry = entries[i];
                var rank = i + 1;
                var difficultyText = GetDifficultyText(entry.Difficulty);
                var item = $"{rank}. {entry.PlayerName} - {entry.Score}分 ({difficultyText}) - {entry.Date:yyyy-MM-dd HH:mm}";
                leaderboardListBox.Items.Add(item);
            }
        }

        private string GetDifficultyText(GameDifficulty difficulty)
        {
            switch (difficulty)
            {
                case GameDifficulty.Easy:
                    return "简单";
                case GameDifficulty.Medium:
                    return "中等";
                case GameDifficulty.Hard:
                    return "困难";
                default:
                    return "未知";
            }
        }

        private void difficultyComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadLeaderboard();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
} 